package com.tiem_files.service.impl;

import com.tiem_files.dao.UserDao;
import com.tiem_files.dao.UserFocusDao;
import com.tiem_files.dao.UserInfoDao;
import com.tiem_files.model.User;
import com.tiem_files.model.UserFocus;
import com.tiem_files.model.UserInfo;
import com.tiem_files.service.UserService;
import com.tiem_files.utils.MailUtil;
import com.tiem_files.utils.StrUtil;


import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDao();

    private UserInfoDao userInfoDao = new UserInfoDao();

    private UserFocusDao userFocusDao = new UserFocusDao();
    @Override
    public User finByName(String username) {
        List<User> userList = userDao.findByCol("username",username);
        if (userList.isEmpty()){
            return null;
        }else {
            return userList.get(0);
        }
    }

    @Override
    public User finByEmail(String email) {
        List<User> userList = userDao.findByCol("email",email);
        if (userList.isEmpty()){
            return null;
        }else {
            return userList.get(0);
        }
    }

    @Override
    public boolean checkByEmailOrName(String email, String username) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("email",email);
        List<User> userList =  userDao.findByCols("or", map);
        return userList.isEmpty();
        /*if (userList.isEmpty()) {
            return true;
        }else {
            return false;
        }*/

    }

    @Override
    public boolean addUser(User user) {
        if (userDao.save(user) > 0){
            Map<String,Object> map =userDao.getBaseInfoByName(user.getUsername());
            //添加用户info表信息
            UserInfo info = new UserInfo();
            info.setUserId(Integer.parseInt(map.get("user_id").toString()));
            info.setRealName("荏苒");
            info.setRegisterTime(new Date());
            return userInfoDao.save(info) > 0;
        }
       return false;
    }

    @Override
    public String sendEmailCode(String email) {
        String code = StrUtil.getRan(6);
        String subject ="《小标交友》 重置密码";
        String content ="您的验证码是："+code+"十分钟内有效";
        try {
            MailUtil.send(email,subject,content,MailUtil.UNICODE);
        } catch (MessagingException | UnsupportedEncodingException e) {
                e.printStackTrace();
        }
        return code;
    }


    @Override
    public boolean resetPassword(String  email, String password) {
        List<User> users =  userDao.findByCol("email", email);
        if (! users.isEmpty()) {
            User user=users.get(0);
            user.setPassword(password);
           return userDao.update(user) > 0;
        }
        return false;
    }

    @Override
    public Map<String,Object>  findUserBaseInfo(String username) {
        return userDao.getBaseInfoByName(username);
    }

    @Override
    public Map<String, Object> getUserInfo(String id) {
        return userDao.getUserInfoById(id);
    }

    @Override
    public void updateLoginTime(Integer userId) {
        UserInfo info = userInfoDao.findById(userId);
       if (null != info){
           info.setLoginTime(new Date());
           userInfoDao.update(info);
       }
    }

    @Override
    public List<Map<String, Object>> getUserList(String userId,int currentPage, String name) {
        userDao.findUsers(userId,currentPage,name);
        return null;
    }

    @Override
    public long getUserCount(String name) {

        return userDao.getUserTotal(name);
    }

    @Override
    public boolean followUser(String userId, String ufId) {
        UserFocus focus = new UserFocus();
        focus.setUserId(Integer.parseInt(userId));
        focus.setUserFocusId(Integer.parseInt(ufId));
        return userFocusDao.save(focus) > 0;
    }

    @Override
    public UserFocus getFollowUser(String userId, String ufId) {
        Map<String,Object> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("user_focus_id",ufId);
        List<UserFocus> focusList = userFocusDao.findByCols("and", map);
        if (!focusList.isEmpty()){
            return focusList.get(0);
        }
        return null;
    }

    @Override
    public boolean unFollowUser(Integer id) {
        return userFocusDao.deleteById(id) > 0;
    }

    @Override
    public void uploadImg(String userId, String path) {
        UserInfo info = userInfoDao.findById(Integer.parseInt(userId));
        info.setPic(path);
        userInfoDao.update(info);
    }
}
